import { createRouter, createWebHashHistory } from 'vue-router'
import { message } from 'ant-design-vue'
import Login from '@/components/Login.vue'
import Register from '@/components/Register.vue'
import ForgotPassword from '@/components/ForgotPassword.vue'
import Help from '@/views/Help.vue'
import Privacy from '@/views/Privacy.vue'
import TeacherHome from '@/views/TeacherHome.vue'
import Home from '@/views/Home.vue'
import OJ from '@/views/OJ.vue'
import OJProblem from '@/views/OJProblem.vue'
import OJJudgeResult from '@/views/OJJudgeResult.vue'
import Profile from '@/views/Profile.vue'
import UpdatePhone from '@/views/UpdatePhone.vue'
import UpdateEmail from '@/views/UpdateEmail.vue'
import Notification from '@/views/Notification.vue'
import Project from '@/views/Project.vue'
import Blog from '@/views/Blog.vue'
import WriteBlog from '@/views/WriteBlog.vue'
import Achievement from '@/views/Achievement.vue'
import ProjectProblem from '@/views/ProjectProblem.vue'
import ProjectReview from '@/views/ProjectReview.vue'
import Index from '@/views/Index.vue'

const routes = [
  {
    path: '/',
    name: 'index',
    component: Index,
    meta: {
      title: '首页'
    }
  },
  {
    path: '/login',
    name: 'login',
    component: Login,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/home',
    name: 'home',
    // component: () => {
    //   const userRole = localStorage.getItem('userRole')
    //   return userRole === 'TEACHER' ? TeacherHome : Home
    // },
    component: TeacherHome,
    meta: {
      title: '首页',
      requiresAuth: true
    }
  },
  {
    path: '/register',
    name: 'register',
    component: Register,
    meta: {
      title: '注册'
    }
  },
  {
    path: '/forgot-password',
    name: 'forgotPassword',
    component: ForgotPassword,
    meta: {
      title: '忘记密码'
    }
  },
  {
    path: '/help',
    name: 'help',
    component: Help,
    meta: {
      title: '帮助'
    }
  },
  {
    path: '/privacy',
    name: 'privacy',
    component: Privacy,
    meta: {
      title: '隐私条款'
    }
  },
  {
    path: '/terms',
    name: 'terms',
    component: () => import('@/views/Terms.vue'),
    meta: {
      title: '服务条款'
    }
  },
  {
    path: '/project-review/:id',
    name: 'projectReview',
    component: ProjectReview,
    meta: {
      title: '项目审批',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/settings',
    name: 'settings',
    component: () => import('@/views/Settings.vue'),
    meta: {
      title: '设置',
      requiresAuth: true
    }
  },
  {
    path: '/oj',
    name: 'oj',
    component: OJ,
    meta: {
      title: 'OJ练习',
      requiresAuth: true
    }
  },
  {
    path: '/oj/problem/:id',
    name: 'ojProblem',
    component: OJProblem,
    meta: {
      title: 'OJ答题',
      requiresAuth: true
    }
  },
  {
    path: '/oj/judge-submit/:id',
    name: 'ojJudgeSubmit',
    component: OJJudgeResult,
    meta: {
      title: 'OJ提交结果',
      requiresAuth: true
    }
  },
  {
    path: '/profile',
    name: 'profile',
    component: Profile,
    meta: {
      title: '个人信息',
      requiresAuth: true
    }
  },
  {
    path: '/update-phone',
    name: 'updatePhone',
    component: UpdatePhone,
    meta: {
      title: '修改手机',
      requiresAuth: true
    }
  },
  {
    path: '/update-email',
    name: 'updateEmail',
    component: UpdateEmail,
    meta: {
      title: '修改邮箱',
      requiresAuth: true
    }
  },
  {
    path: '/notification',
    name: 'notification',
    component: Notification,
    meta: {
      title: '消息通知',
      requiresAuth: true
    }
  },
  {
    path: '/project',
    name: 'project',
    component: Project,
    meta: {
      title: '项目实践',
      requiresAuth: true
    }
  },
  {
    path: '/blog',
    name: 'blog',
    component: Blog,
    meta: {
      title: '博客',
      requiresAuth: true
    }
  },
  {
    path: '/blog/write',
    name: 'writeBlog',
    component: WriteBlog,
    meta: {
      title: '写博客',
      requiresAuth: true
    }
  },
  {
    path: '/achievement',
    name: 'achievement',
    component: Achievement,
    meta: {
      title: '我的成就',
      requiresAuth: true
    }
  },
  {
    path: '/project-manage',
    name: 'projectManage',
    component: () => import('@/views/ProjectManage.vue'),
    meta: {
      title: '项目管理',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/project-create',
    name: 'projectCreate',
    component: () => import('@/views/ProjectCreate.vue'),
    meta: {
      title: '新增项目',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/achievement-manage',
    name: 'achievementManage',
    component: () => import('@/views/AchievementManage.vue'),
    meta: {
      title: '成就管理',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/achievement-detail/:id',
    name: 'achievementDetail',
    component: () => import('@/views/AchievementDetail.vue'),
    meta: {
      title: '成就详情',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/project/problem/:id',
    name: 'projectProblem',
    component: ProjectProblem,
    meta: {
      title: '项目实践',
      requiresAuth: true
    }
  },
  {
    path: '/class-manage',
    name: 'classManage',
    component: () => import('@/views/ClassManage.vue'),
    meta: {
      title: '班级管理',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/class-create',
    name: 'classCreate',
    component: () => import('@/views/ClassCreate.vue'),
    meta: {
      title: '新增班级',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/class-detail/:id',
    name: 'classDetail',
    component: () => import('@/views/ClassDetail.vue'),
    meta: {
      title: '班级详情',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/course-manage',
    name: 'courseManage',
    component: () => import('@/views/CourseManage.vue'),
    meta: {
      title: '课程管理',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/course-create',
    name: 'courseCreate',
    component: () => import('@/views/CourseCreate.vue'),
    meta: {
      title: '新增课程',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/course-detail/:id',
    name: 'courseDetail',
    component: () => import('@/views/CourseDetail.vue'),
    meta: {
      title: '课程详情',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/course-edit/:id',
    name: 'courseEdit',
    component: () => import('@/views/CourseEdit.vue'),
    meta: {
      title: '编辑课程',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  },
  {
    path: '/class-edit/:id',
    name: 'classEdit',
    component: () => import('@/views/ClassEdit.vue'),
    meta: {
      title: '编辑班级',
      requiresAuth: true,
      roles: ['TEACHER']
    }
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  // 设置页面标题
  document.title = to.meta.title ? `${to.meta.title} - 实践项目教学支持系统` : '实践项目教学支持系统'

  const token = localStorage.getItem('token')
  const userRole = localStorage.getItem('userRole')

  // 处理需要认证的路由
  if (to.meta.requiresAuth) {
    if (!token) {
      message.warning('请先登录')
      next({ name: 'login', query: { redirect: to.fullPath } })
      return
    }

    // 检查角色权限
    if (to.meta.roles && !to.meta.roles.includes(userRole)) {
      message.error('没有访问权限')
      next({ name: 'home' })
      return
    }
  }

  // 如果已登录，访问登录页则重定向到首页
  if (token && (to.name === 'login' || to.name === 'register' || to.name === 'forgotPassword')) {
    next({ name: 'home' })
    return
  }

  next()
})

export default router 